/*DONE* Table [dbo].[<CompanyName>$Customer$5ecfc871-5d82-43f1-9c54-59685e82318d]  2/2  Script Date: 28.7.2020 - JI ******/
/*Add prestaging tables for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT MAX([Company]) FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Customer$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Customer$5ecfc871-5d82-43f1-9c54-59685e82318d](
    [timestamp] [timestamp] NOT NULL,
	[No_] [nvarchar](20) NOT NULL,
	[LSC Date Created] [datetime] NOT NULL,
	[LSC Created by User] [nvarchar](50) NOT NULL,
	[LSC Customer ID] [nvarchar](10) NOT NULL,
	[LSC Reason Code] [nvarchar](10) NOT NULL,
	[LSC Restriction Functionality] [int] NOT NULL,
	[LSC Print Document Invoice] [tinyint] NOT NULL,
	[LSC Transaction Limit] [decimal](38, 20) NOT NULL,
	[LSC Daytime Phone No_] [nvarchar](30) NOT NULL,
	[LSC Mobile Phone No_] [nvarchar](30) NOT NULL,
	[LSC House_Apartment No_] [nvarchar](30) NOT NULL,
	[LSC Retail Customer Group] [nvarchar](10) NOT NULL,
	[LSC Default Weight] [decimal](38, 20) NOT NULL,
	[LSC Other Tender in Finalizing] [tinyint] NOT NULL,
	[LSC Post as Shipment] [tinyint] NOT NULL,
	[LSC AmtChargedOnPOSInt] [decimal](38, 20) NOT NULL,
	[LSC AmtChargedPostedInt] [decimal](38, 20) NOT NULL,
	[LSC BalanceLCYInt] [decimal](38, 20) NOT NULL,
 CONSTRAINT ['+@Company+'$Customer$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i+1
END